package com.common.www.controller.recycle;

import com.alibaba.fastjson.JSON;
import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.district.ProvinceCity;
import com.common.www.bean.entity.recycle.RecycleType;
import com.common.www.bean.entity.recycle.RecycleTypePrice;
import com.common.www.controller.base.DefaultCrudController;
import com.common.www.data.NetCode;
import com.common.www.service.auth.UserService;
import com.common.www.service.district.ProvinceCityService;
import com.common.www.service.recycle.RecycleTypePriceService;
import com.common.www.service.recycle.RecycleTypeService;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
@RequestMapping(value = {"mgt/recycleTypePrice"})
@Api(tags = "MGT 回收品类管理接口")
public class MGTRecycleTypePriceController extends DefaultCrudController<RecycleTypePrice, Long> {
    @Autowired
    private RecycleTypeService service;
    @Autowired
    private RecycleTypePriceService priceService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProvinceCityService provinceCityService;

    protected IBaseService getService() {
        return priceService;
    }


    @ApiOperation("添加或修改回收品类价格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJson", method = RequestMethod.POST)
    public Result saveByJson(String json) {
        RecycleTypePrice entity = JSON.parseObject(json, RecycleTypePrice.class);
        ProvinceCity provinceCity = provinceCityService.getRepository().findByFlagAndCode(1, Long.valueOf(entity.getCode()));
        if (provinceCity == null) {
            throw new RuntimeException("未找到所选城市，请联系管理员");
        }
        if (provinceCity.getParent() != null){
            entity.setProvince(provinceCity.getParent().getName());
        }
        entity.setCity(provinceCity.getName());

        HashMap criteria = new HashMap();
        criteria.put("recycleType.id", entity.getRecycleType().getId());
        criteria.put("code", provinceCity.getCode());
        List<RecycleTypePrice> existingEntities = priceService.getList(criteria, null);

        // 如果存在记录，则进行更新
        if (existingEntities.size() > 0) {
            RecycleTypePrice existingEntity = existingEntities.get(0);
            if (entity.getId() != null && entity.getId().equals(existingEntity.getId())) {
                existingEntity.setMinPrice(entity.getMinPrice());
                existingEntity.setMaxPrice(entity.getMaxPrice());
                existingEntity.setSuggestPrice(entity.getSuggestPrice());
                existingEntity.setRemark(entity.getRemark());
                existingEntity.setCode(entity.getCode());
                existingEntity.setProvince(entity.getProvince());
                existingEntity.setCity(entity.getCity());
                existingEntity.setUnitType(entity.getUnitType());

                existingEntity.setRecycleType(entity.getRecycleType());
                existingEntity.setImg(entity.getImg());

                entity = priceService.save(existingEntity);
            } else {
                throw new RuntimeException("当前城市已存在该品类的价格信息");
            }
        } else {
            // 如果不存在记录，则进行新增
            entity.setCode(provinceCity.getCode().toString());
            entity = priceService.save(entity);
        }
        return R.ok(entity);
    }


    @ApiOperation(value = "逻辑删除", notes = "参数为JSON数据对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long", paramType = "query"),
    })
    @RequestMapping(value = "/deleteByLogic", method = RequestMethod.POST)
    public Result deleteByLogic(Long id) {
        priceService.deleteById(id);
        return R.ok();
    }


    @ApiOperation("批量删除启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "String", defaultValue = ""),
            @ApiImplicitParam(name = "operate", value = "1.删除 2.启用", defaultValue = ""),
    })
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public Result batchDelete(Long[] ids, Integer operate) {
        if (ParamsUtils.isInputEmpty(ids, operate)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        for (int i = 0; i < ids.length; i++) {
            RecycleType authUser = service.findOne(ids[i]);
            if (authUser != null) {
                if (operate == 1) {
                    authUser.setFlag(0);
                } else if (operate == 2) {
                    authUser.setFlag(1);
                }
                service.save(authUser);
            }
        }
        return R.ok();
    }


    @ApiOperation(value = "逻辑删除批量", notes = "参数为JSON数据对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "8,28", dataType = "long", paramType = "query"),
    })
    @RequestMapping(value = "/deletesByLogic", method = RequestMethod.POST)
    public Result deletesByLogic(Long[] ids) {
        service.deleteByIds(ids);
        return R.ok();
    }


}

